<?php


namespace app\index\model;


use think\Db;
use think\Model;

class Business extends Model
{
    /**
     * @setMalSel   查找在附近的商家
     * @param string $field
     * @param string $position
     * @return mixed
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public function setMalSel(string $field,string $position){
        return $this->query("SELECT $field from business where concat(provide,city,county,street) like '$position%'");
    }

    /**
     * @youLikeBusiness 猜你喜欢商家
     * @return Business[]
     */
    public function youLikeBusiness(){
        return $this->field('id,businessName,county')->select();
    }
    /**
     * @BusinessInfo    商家页信息
     * @param int $id
     * @return Business
     */
    public function BusinessInfo(int $id){
        return $this ->where('id',$id)
            ->field('businessName,businessImg,provide,city,county,street,businessAddress,startTime,endTime,deliverMoney,startDeliver')
            ->find();
    }
    /**
     * @randBusiness    随机商家
     * @return mixed
     */
    public function randBusiness(string $str){
        return $this->field($str)->select();
    }
    /**
     * @rankingList 首页-排行榜
     * @return Business[]
     */
    public function rankingList(){
        return $this->field('id,businessName,businessImg,businessHits')
            ->limit(3)
            ->order('businessHits','desc')
            ->select();
    }

    /**
     * @findBusiness    查找商家
     * @param int $id
     * @return mixed
     */
    //查找商家名
    public function findBusiness(int $id){
        return $this->where('id',$id)->value('businessName');
    }
    public  function  selb(string $key,string $what,string $value){
        // TODO 请统一使用model类，不使用助手函数
        $data = $this
            ->table('business')
            ->field($value)
            ->where($key,"=", $what)
            ->select();
        return $data;
    }
    /**
     * @param int $id
     * @return mixed
     */
    //查找商家头像(通过商家id)
    public function findBusinessImg(int $id){
        return $this->where('id',$id)->value('businessImg');
    }

    /**
     * @param int $id
     * @return mixed
     */
    //找到img,range,name(history页面)
    public function findHistoryInfo(int $id){
        return $this->field('businessName,businessImg,businessRange')->where('id',$id)->select();
    }

    /**
     * @param int $id
     * @return mixed
     */
    //找到img,name,deliverMoney,startDeliver(favorites页面)
    public function findFavoritesInfo(int $id){
        return $this->field('businessName,businessImg,deliverMoney,startDeliver')->where('id',$id)->select();
    }


    /**
     * @param int $id
     * @return mixed
     */
    public function findBusinessadd(int $id){
        // TODO 请统一使用model类，不使用助手函数
        return  $this
            ->table('business')
            ->field('businessName,provide,city,county,street,businessAddress,businessImg')
            ->where('id',"=", $id)
            ->select();
    }
    /**搜索结果
     * @param $input
     * @param $sort
     * @return array|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function searchBusiness(string $input,string $sort){
        // TODO 模型只操作数据库，有关逻辑性操作在控制器操作完传进，请统一使用model类，不使用助手函数
        $data = $this
            ->table('business')
            ->field('id,businessName,businessImg,provide,city,county,street,businessAddress,businessHits')
            ->whereLike('businessName',["%$input%"],'and')
            ->where('deleted','=','0')
            ->order('businessHits' , "$sort")
            ->select();
        return $data;
    }

    public function searchBusinessplace(string $input,string $sort,string $place){
        // TODO 模型只操作数据库，有关逻辑性操作在控制器操作完传进，请统一使用model类，不使用助手函数

        $data = $this
            ->table('business')
            ->field('id,businessName,businessImg,provide,city,county,street,businessAddress,businessHits')
            ->whereLike('businessName',["%$input%"],'and')
            ->whereLike('county',["%$place%"],'and')
            ->where('deleted','=','0')
            ->order('businessHits' , "$sort")
            ->select();
        return $data;
    }

    public function findBusinessImgName(int $businessId){
        return $this->field('businessName,businessImg')->where('id',$businessId)->find();
    }

    /**
     * @param int $userId
     * @param string $businessPassword
     * @param int $businessType
     * @param string $businessImg
     * @param string $businessLicenceImg
     * @param string $licenceupImg
     * @param string $licencedownImg
     * @param string $businessName
     * @param int $phone
     * @param int $deliverMoney
     * @param int $startDeliver
     * @param string $startTime
     * @param string $endTime
     * @param string $provide
     * @param string $city
     * @param string $county
     * @param string $street
     * @param string $businessAddress
     * @param string $businessDesc
     * @return int|string
     */
    //店铺入驻
    public function enterStoreInformation(int $userId,string $businessImg,string $businessLicenceImg,string $licenceupImg,string $licencedownImg,string $businessPassword,int $businessType,string $businessName,int $phone,int $deliverMoney,int $startDeliver,string $startTime,string $endTime,string $provide,string $city,string $county,string $street,string $businessAddress,string $businessDesc){
        $data = [
            'userId'                => $userId,
            'businessImg'           => $businessImg,
            'businessLicenceImg'    => $businessLicenceImg,
            'licenceupImg'          => $licenceupImg,
            'licencedownImg'        => $licencedownImg,
            'businessName'          => $businessName,
            'businessPassword'      => $businessPassword,
            'phone'                 => $phone,
            'businessDesc'          => $businessDesc,
            'businessType'          => $businessType,
            'provide'               => $provide,
            'city'                  => $city,
            'county'                => $county,
            'street'                => $street,
            'businessAddress'       => $businessAddress,
            'deliverMoney'          => $deliverMoney,
            'startDeliver'          => $startDeliver,
            'startTime'             => $startTime,
            'endTime'               => $endTime,
            'createTime'            => time(),
            'updateTime'            => time()
        ];
        return $this->insert($data);
    }


    /**
     * @param int $userId
     * @return mixed
     */
        //查询是否店铺已入住
    public function findStore(int $userId){
        return $this->where('userId',$userId)->select()->count();
    }
    public function youLikecounty(int $businessId){
        return $this->field('county')->where('id',$businessId)->select();
    }
}